package cn.it58.gooditname.services

import com.intellij.ide.util.PropertiesComponent
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.Service

@Service
class ConfigService {

    companion object {
        private const val API_KEY_PROPERTY = "goodit.apiKey"
        private const val AI_MODEL_PROPERTY = "goodit.aiModel"
        private const val ENABLE_API_PROPERTY = "goodit.enableApi"
        private const val FALLBACK_LOCAL_PROPERTY = "goodit.fallbackLocal"

        private const val DEFAULT_AI_MODEL = "Qwen/Qwen2.5-7B-Instruct"

        fun getInstance(): ConfigService {
            return ApplicationManager.getApplication().getService(ConfigService::class.java)
        }
    }

    /**
     * 获取API密钥
     */
    fun getApiKey(): String {
        return PropertiesComponent.getInstance().getValue(API_KEY_PROPERTY, "")
    }

    /**
     * 设置API密钥
     */
    fun setApiKey(apiKey: String) {
        PropertiesComponent.getInstance().setValue(API_KEY_PROPERTY, apiKey)
    }

    /**
     * 清除API密钥
     */
    fun clearApiKey() {
        PropertiesComponent.getInstance().unsetValue(API_KEY_PROPERTY)
    }

    /**
     * 获取AI模型
     */
    fun getAiModel(): String {
        return PropertiesComponent.getInstance().getValue(AI_MODEL_PROPERTY, DEFAULT_AI_MODEL)
    }

    /**
     * 设置AI模型
     */
    fun setAiModel(aiModel: String) {
        PropertiesComponent.getInstance().setValue(AI_MODEL_PROPERTY, aiModel)
    }

    /**
     * 是否启用API翻译
     */
    fun isApiEnabled(): Boolean {
        return PropertiesComponent.getInstance().getBoolean(ENABLE_API_PROPERTY, true)
    }

    /**
     * 设置是否启用API翻译
     */
    fun setApiEnabled(enabled: Boolean) {
        PropertiesComponent.getInstance().setValue(ENABLE_API_PROPERTY, enabled)
    }

    /**
     * 是否启用本地回退
     */
    fun isFallbackLocalEnabled(): Boolean {
        return PropertiesComponent.getInstance().getBoolean(FALLBACK_LOCAL_PROPERTY, true)
    }

    /**
     * 设置是否启用本地回退
     */
    fun setFallbackLocalEnabled(enabled: Boolean) {
        PropertiesComponent.getInstance().setValue(FALLBACK_LOCAL_PROPERTY, enabled)
    }

    /**
     * 检查API密钥是否已配置
     */
    fun isApiKeyConfigured(): Boolean {
        val apiKey = getApiKey()
        return apiKey.isNotBlank()
    }
} 